<?php

function getConfigValue($name)
{
    return \app\common\model\Config::where('name',$name)->value('value');
}
function nf($number)
{
    return number_format($number,3,'.','');
}
function getParents($user_id, $field = 'id,level', $limit = '')
{
    $path = \app\common\model\User::where('id', $user_id)->value('path');
    $path_arr = explode('-', $path);
    array_shift($path_arr);
    $user_data = [];
    foreach ($path_arr as $val) {
        $user_data[] = app\common\model\User::field($field)->where('id',$val)->find();
    }
//    $user_data = \app\common\model\User::field($field)->whereIn('id', $path_arr)
//        ->order('id', 'desc');
//    if (!empty($limit)) {
//        $user_data = $user_data->limit($limit);
//    }
//    $user_data = $user_data->select();
    return $user_data;
}

function getParentsIds($user_id, $limit = '')
{
    $path = \app\common\model\User::where('id', $user_id)->value('path');
    $path_arr = explode('-', $path);
    array_shift($path_arr);

//    $user_data = \app\common\model\User::whereIn('id', $path_arr)
//        ->order('id', 'desc');
//    if (!empty($limit)) {
//        $user_data = $user_data->limit($limit);
//    }
//    $user_data = $user_data->column('id');
    return $path_arr;
}

function getChilds($user_id, $field = 'id,level')
{
    $user_data = \app\common\model\User::where('path', 'like', '%-' . $user_id . '-%')
        ->whereOr('path', 'like', '%-' . $user_id)
        ->field($field)
        ->select();

    return $user_data;
}

function getChildsIds($user_id)
{
    $user_data = \app\common\model\User::where('path', 'like', '%-' . $user_id . '-%')
        ->whereOr('path', 'like', '%-' . $user_id)
//        ->field($field)
        ->column('id');

    return $user_data;
}